﻿//Copyright 2014 Thomas Rega

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;

namespace TinyDeduplicator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static FolderBrowserDialog folderBrowser = new FolderBrowserDialog();

        internal static Options options = new Options();

        public MainWindow()
        {
            InitializeComponent();
            lblStatus.Content = "Version " + Assembly.GetExecutingAssembly().GetName().Version;
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtLocation.Text = folderBrowser.SelectedPath;
            }
        }

        private void btnScan_Click(object sender, RoutedEventArgs e)
        {
            runScan();
        }

        private void txtLocation_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                runScan();
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DeletionManager manager = new DeletionManager(dataGrid.SelectedItems);

            if (manager.containsFiles())
            {
                System.Windows.MessageBox.Show("No items are currently selected for deletion.");
            }
            else
            {
                runDelete(manager);
            }
        }

        private void btnOpenFileLocation_Click(object sender, RoutedEventArgs e)
        {
            DeletionManager manager = new DeletionManager(dataGrid.SelectedItems);

            if (manager.containsFiles())
            {
                System.Windows.MessageBox.Show("No items are currently selected.");
            }
            else
            {
                runOpenFileLocation(manager);
            }
        }

        private void txbLicense_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (System.Windows.MessageBox.Show(
                    "Copyright 2014 Thomas Rega\n" +
                    "\n" +
                    "Licensed under the Apache License, Version 2.0 (the \"License\"); " +
                    "you may not use this application except in compliance with the License.\n" +
                    "You may obtain a copy of the License at\n" +
                    "\n" +
                    "http://www.apache.org/licenses/LICENSE-2.0\n" +
                    "\n" +
                    "Unless required by applicable law or agreed to in writing, software " +
                    "distributed under the License is distributed on an \"AS IS\" BASIS, " +
                    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
                    "See the License for the specific language governing permissions and" +
                    "limitations under the License.\n" +
                    "\n" +
                    "Would you like to view the full license now?",
                    "License",
                    MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Process.Start("http://www.apache.org/licenses/LICENSE-2.0");
            }
        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {
            OptionsWindow optionsWindow = new OptionsWindow(options);
            optionsWindow.Left = this.Left + (this.ActualWidth - optionsWindow.Width) / 2;
            optionsWindow.Top = this.Top + (this.ActualHeight - optionsWindow.Height) / 2;

            if ((bool)optionsWindow.ShowDialog())
            {
                options = optionsWindow.options;
            }
        }
    }
}
